#include "delivery.h"
#include "ui_delivery.h"

delivery::delivery(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::delivery)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(),this->height());

    QSqlQuery sqlquery;
    sqlquery.prepare("SELECT stockid FROM stock_info;");
    if(sqlquery.exec())
    {
        while(sqlquery.next())
        {
            QString id = sqlquery.value(0).toString();
            ui->comboBox->addItem(id);
        }
    }
}

delivery::~delivery()
{
    delete ui;
}

void delivery::on_pushButtonDelivery_clicked()
{
    int id = ui->comboBox->currentText().toInt();
    int subnumber = ui->lineEdit->text().toInt();

    QSqlQuery sqlquery;
    sqlquery.prepare("SELECT stockamount FROM stock_info WHERE stockid = :id ;");
    sqlquery.bindValue(":id",id);
    if(sqlquery.exec())
    {
        int number = 0;
        while(sqlquery.next())
        {
            number = sqlquery.value(0).toInt();
        }
        if(subnumber>number)
        {
            QMessageBox::information(this,"提示","出库失败，商品数量不足！！",QMessageBox::Yes);
        }
        else
        {
            sqlquery.prepare("UPDATE stock_info SET stockamount = stockamount - :number WHERE stockid = :goodsid");
            sqlquery.bindValue(":number",subnumber);
            sqlquery.bindValue(":goodsid",id);
            if(sqlquery.exec())
            {
                QMessageBox::information(this,"提示","恭喜你，商品出库成功！！",QMessageBox::Yes);
            }


            sqlquery.prepare("SELECT * FROM stock_info WHERE stockid = :id;");
            sqlquery.bindValue(":id",id);
            if(sqlquery.exec())
            {
                while(sqlquery.next())
                {
                    QString stockid = sqlquery.value(0).toString();
                    QString stockname = sqlquery.value(1).toString();
                    int amount = subnumber;
                    int stockprice = sqlquery.value(3).toInt();
                    QString stocksupplice = sqlquery.value(4).toString();
                    QString stockdirector = sqlquery.value(5).toString();
                    QString stockdeliverytime = sqlquery.value(7).toString();
                    QString stockremarks = sqlquery.value(8).toString();
                    QSqlQuery sql;

                    sql.prepare("INSERT INTO "
                                     "outputdata(product_id,product_name,product_quantity,unit_price,customer_name,responsible_person,output_time,remarks) "
                                     "VALUES(:stockid,:stockname,:amount,:stockprice,:stocksupplice,:stockdirector,:stockdeliverytime,:stockremarks);");
                    sql.bindValue(":stockid",stockid);
                    sql.bindValue(":stockname",stockname);
                    sql.bindValue(":amount",amount);
                    sql.bindValue(":stockprice",stockprice);
                    sql.bindValue(":stockdirector",stockdirector);
                    sql.bindValue(":stocksupplice",stocksupplice);
                    sql.bindValue(":stockdeliverytime",stockdeliverytime);
                    sql.bindValue(":stockremarks",stockremarks);

                    if(!sql.exec())
                    {
                        QMessageBox::information(this,"提示","出库失败失败",QMessageBox::Yes);
                    }
                }
            }

        }
    }

}

void delivery::on_pushButtonEsc_clicked()
{
    close();
}
